
from dataclasses import dataclass, field
from datetime import datetime
from typing import List

from sqlalchemy import *
from sqlalchemy.orm import relationship

# from orm_domain import suite_testcase_rel
from orm_domain.suite_testcase_rel import suite_testcase_rel
from server import mapper_registry
from orm_domain.job_suite_rel import job_suite_rel


# class Suite(db.Model):
#     '''
#     测试套件表
#     '''
#
#     __tablename__ = 'suite'
#     id = db.Column(db.Integer,primary_key=True)
#     name = db.Column(String(255))
#     project_id = db.Column(db.Integer,ForeignKey('project.id'))
#     serial_parallel = db.Column(db.Integer)
#     create_time = db.Column(db.TIMESTAMP())
#     update_time = db.Column(db.TIMESTAMP())
#     # 复数表示多对多关系
#     testcases = db.relationship("Testcase", secondary=suite_testcase_rel, backref="suites")
#     # job 和 suite 也是多对多的关系
#     jobs = relationship('Job', secondary=job_suite_rel)


@mapper_registry.mapped
@dataclass
class Suite:
    __tablename__ = 'suite'
    __sa_dataclass_metadata_key__ = "sa"

    id: int = field(default=None, metadata={"sa": Column(Integer, primary_key=True)}, )
    name: str = field(default=None, metadata={"sa": Column(String(255))}, )
    project_id: int = field(default=None, metadata={"sa": Column(Integer,ForeignKey('project.id'))}, )
    serial_parallel: int = field(default=None, metadata={"sa": Column(Integer)}, )
    create_time: datetime = field(init=False, metadata={"sa": Column(DateTime, nullable=True, default=datetime.now)}, )
    update_time: datetime = field(init=False, metadata={"sa": Column(DateTime, nullable=True, default=datetime.now, onupdate=datetime.now)}, )
    testcases:[] = field(init=False, metadata={"sa": relationship("Testcase", secondary=suite_testcase_rel, backref="suites",order_by='Testcase.update_time.desc()')} )
    jobs:[] = field(init=False, metadata={"sa": relationship('Job', secondary=job_suite_rel)} )